"use strict";
const base64Buffer = function(buffer) {
	console.log("buffer经过base64编码之后是", buffer.toString("base64"));
	return buffer.toString("base64");
};

const encrypt = function(str) {
	return new Promise((success, fail) => {
		const chunks = [];
		let length = 0;
		const sha256 = require("crypto").createHash("sha256");
		sha256.write(str);
		sha256.end();
		sha256.on("data", chunk => {
			chunks.push(chunk);
			length += chunk.length;
		});
		console.log("签名的原始内容是", str);
		sha256.on("end", () => {
			const afterEncryption = Buffer.concat(chunks, length);
			console.log("sha256之后用16进制表示是", afterEncryption.toString("hex"));
			success(base64Buffer(afterEncryption));
		});
		sha256.on("error", e => {
			console.error("sha256发生错误", e);
			fail(e);
		});
	});
};

module.exports = {
	encrypt
};